package com.ruoyi.product.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.TypeReference;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.product.config.RabbitMQConfig;
import com.ruoyi.product.domain.*;
import com.ruoyi.product.feign.UserfeignService;
import com.ruoyi.product.service.IMycommentService;
import com.ruoyi.product.service.QuanZiApi;
import com.ruoyi.product.service.impl.MycommentServiceImpl;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.product.service.IProductCommentService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 用户评论表Controller
 *
 * @author 林道敬
 * @date 2025-01-07
 */
@RestController
@RequestMapping("/product_comment")
public class ProductCommentController extends BaseController
{
    @Autowired
    private IProductCommentService productCommentService;
    @Autowired
    private QuanZiApi quanZiApi;
    @Autowired
    UserfeignService userfeignService;
    @Autowired
    RabbitTemplate rabbitTemplate;

    /**
     * 点赞
     *
     * @param publishId
     * @return
     */
    @GetMapping("/like/{publishId}")
    public R likeComment(@PathVariable("publishId") String publishId) {
        System.out.println("进入likeComment");
        String userId = "1";
        try {
            Boolean operate = productCommentService.likeComment(Long.valueOf(userId), publishId);
            if(operate){
                PointMessage pointMessage = new PointMessage();
                pointMessage.setScore(5);
                pointMessage.setPublishId(Long.parseLong(publishId));
                rabbitTemplate.convertAndSend(RabbitMQConfig.ITEM_TOPIC_EXCHANGE ,"item.like" ,pointMessage);
                return R.ok("点赞成功");
            }else {
                return R.fail("点赞失败或者已经点赞过");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.fail("点赞失败");
    }

    /**
     * 取消点赞
     *
     * @param publishId
     * @return
     */
    @GetMapping("/dislike/{publishId}")
    public R disLikeComment(@PathVariable("publishId") String publishId) {
        String userId = "1";
        try {
            Boolean operate = productCommentService.disLikeComment(Long.valueOf(userId), publishId);
            if(operate){
                PointMessage pointMessage = new PointMessage();
                pointMessage.setScore(-5);
                pointMessage.setPublishId(Long.parseLong(publishId));
                rabbitTemplate.convertAndSend(RabbitMQConfig.ITEM_TOPIC_EXCHANGE ,"item.unlike" ,pointMessage);
                return R.ok("取消点赞成功");
            }else {
                return R.fail("取消点赞失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.fail("取消点赞失败");
    }
    /**
     * 查询用户评论表列表
     */
   // @RequiresPermissions("product:product_comment:list")
    @GetMapping("/list")
    public TableDataInfo list(ProductComment productComment)
    {
        startPage();

        List<ProductComment> list = productCommentService.selectProductCommentList(productComment);
        for(ProductComment comment : list){
            JsonData info = userfeignService.getInfo(Long.valueOf(comment.getUserId()));
            UserInfo userInfo  = info.getData(new TypeReference<UserInfo>(){});
            comment.setUserName(userInfo.getName());
            comment.setUserLevel(userInfo.getUserLevel());
        }
        return getDataTable(list);
    }

    /**
     * 导出用户评论表列表
     */
    @RequiresPermissions("product:product_comment:export")
    @Log(title = "用户评论表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProductComment productComment)
    {
        List<ProductComment> list = productCommentService.selectProductCommentList(productComment);
        ExcelUtil<ProductComment> util = new ExcelUtil<ProductComment>(ProductComment.class);
        util.exportExcel(response, list, "用户评论表数据");
    }
    /**
     * 点赞
     *
     * @param publishId
     * @return
     */
    @GetMapping("/love/{publishId}")
    public R loveComment(@PathVariable("publishId") String publishId) {
        String userId = "1";
        try {
            Boolean operate = productCommentService.loveComment(Long.valueOf(userId), publishId);
            if(operate){
                PointMessage pointMessage = new PointMessage();
                pointMessage.setScore(8);
                pointMessage.setPublishId(Long.parseLong(publishId));
                rabbitTemplate.convertAndSend(RabbitMQConfig.ITEM_TOPIC_EXCHANGE ,"item.love" ,pointMessage);
                return R.ok("喜欢成功");
            }else {
                return R.fail("喜欢失败或者已经喜欢过");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.fail("喜欢失败");
    }
    /**
     * 点赞
     *
     * @param publishId
     * @return
     */
    @GetMapping("/dislove/{publishId}")
    public R disloveComment(@PathVariable("publishId") String publishId) {
        String userId = "1";
        try {
            Boolean operate = productCommentService.disLoveComment(Long.valueOf(userId), publishId);
            if(operate){
                PointMessage pointMessage = new PointMessage();
                pointMessage.setScore(-8);
                pointMessage.setPublishId(Long.parseLong(publishId));
                rabbitTemplate.convertAndSend(RabbitMQConfig.ITEM_TOPIC_EXCHANGE ,"item.unlove" ,pointMessage);
                return R.ok("取消喜欢成功");
            }else {
                return R.fail("取消喜欢失败或者已经喜欢过");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.fail("取消喜欢失败");
    }

    /**
     * 获取用户评论表详细信息
     */
    @RequiresPermissions("product:product_comment:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(productCommentService.selectProductCommentById(id));
    }

    /**
     * 新增用户评论表
     */
    @RequiresPermissions("product:product_comment:add")
    @Log(title = "用户评论表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProductComment productComment)
    {
        return toAjax(productCommentService.insertProductComment(productComment));
    }

    /**
     * 修改用户评论表
     */
    @RequiresPermissions("product:product_comment:edit")
    @Log(title = "用户评论表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProductComment productComment)
    {
        return toAjax(productCommentService.updateProductComment(productComment));
    }

    /**
     * 删除用户评论表
     */
    @RequiresPermissions("product:product_comment:remove")
    @Log(title = "用户评论表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(productCommentService.deleteProductCommentByIds(ids));
    }
}
